package com.tkm.advancedFunctions.inlines

fun main() {
    cost {
        println("Hello1")
        println("Hello2")
    }


    val intArray = IntArray(100) { it }
    intArray.forEach {
        if (it == 30) {
            //  相当于continue，跳出本次循环
            return@forEach
        }
        println(it)
    }

    nonLocalReturn {
        println("in nonLocalReturn")
        //  nonLocalReturn为内联函数，在内联函数中return，相当于在外层直接return
        //  则外部函数，也就是main中的后续代码都不会执行
//        return
    }

    val r = runnable {
        println("runnable run")
        return@runnable
    }
    r.run()

    hello()

    money = 10.0
    println(money)
}

inline fun hello() {
    println("Hello World")
}

//  inline与入参为函数的高阶函数更匹配
inline fun cost(block: () -> Unit) {
    val start = System.currentTimeMillis()
    block()
    val costTime = System.currentTimeMillis() - start
    println("costTime: $costTime")
}

inline fun nonLocalReturn(block: () -> Unit) {
    block()
}

//  crossinline/noinline：进制non-local return
inline fun runnable(crossinline block: () -> Unit): Runnable {
    //  无法确定run方法如何执行
    return Runnable { block() }
}

var pocket: Double = 0.0

var money: Double
    //  内联属性：针对没有backing-field的getter与setter
    inline get() = pocket
    inline set(value) = run { pocket = value }
